//
//  DXButton.m
//  suremodv
//
//  Created by dd on 13-9-19.
//  Copyright (c) 2013年 xiaomuzhi. All rights reserved.
//

#import "DXButton.h"

@implementation DXButton

@synthesize realTitleLabel = _titleLabel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.userInteractionEnabled = NO;
        [self addSubview:_titleLabel];
        
        _titleLabelHighlightedColor = nil;
        [self addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)setTitle:(NSString *)title textColor:(UIColor *)textColor shadowColor:(UIColor *)shadowColor fontSize:(double)fontSize bold:(BOOL)bold
{
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.text = title;
    _titleLabel.font = bold ? [UIFont boldSystemFontOfSize:fontSize] : [UIFont systemFontOfSize:fontSize];
    _titleLabel.textColor = textColor;
    _titleLabelTextColor = textColor;
    _titleLabel.shadowColor = shadowColor;
    _titleLabel.shadowOffset = CGSizeMake(.5, .5);
    
    
}

- (void)setTitle:(NSString *)title
{
    _titleLabel.text = title;
}

- (void)setTitleFrame:(CGRect)frame
{
    _titleLabel.frame = frame;
}

- (void)setTitleHighlightedColor:(UIColor *)color
{
    _titleLabelHighlightedColor = color;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (NSMutableDictionary *)data
{
    if (_data == nil)
    {
        _data = [[NSMutableDictionary alloc] init];
    }
    
    return _data;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    if (object == self && [keyPath isEqualToString:@"highlighted"])
    {
        if (_titleLabelHighlightedColor != nil)
        {
            if (self.state == UIControlStateNormal)
            {
                _titleLabel.textColor = _titleLabelTextColor;
            }
            else
            {
                _titleLabel.textColor = _titleLabelHighlightedColor;
            }
        }
    }
}

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"highlighted"];
}

@end
